package com.hxzy.commons.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hxzy.commons.dao.IAuthDao;
import com.hxzy.commons.model.CmsUser;
import com.hxzy.commons.service.IAuthService;
import com.hxzy.core.base.dao.IBaseDAO;
import com.hxzy.core.base.service.impl.BaseService;
import com.hxzy.core.page.Finder;
import com.hxzy.core.page.entity.PageParams;
import com.hxzy.core.page.entity.Pagination;
import com.hxzy.core.utils.PageParam;
import com.hxzy.core.utils.PageUtil;

 
@SuppressWarnings("unchecked")
@Service
public class AuthServiceImpl extends BaseService implements IAuthService{

	private IAuthDao authDao;
	@Override
	public IBaseDAO getBaseDao() {
		// TODO Auto-generated method stub
		return authDao;
	}
	@Autowired
	public void setAuthDao(IAuthDao authDao) {
		this.authDao = authDao;
	}

	public PageUtil getUserList(PageParam pageParam){
		return this.authDao.getUserList(pageParam);
	}
	public PageUtil getAccList(PageParam pageParam){
		return this.authDao.getAccList(pageParam);
	}
	/**
	 * 根据用户uid获得用户帐号信息
	 * @param uid
	 * @return
	 */
	public CmsUser getAccByUid(String uid){
		return this.authDao.getAccByUid(uid);
	}
	@Override
	public PageUtil loadUserList(PageParam pageParam) {
		return this.authDao.loadUserList(pageParam);
	}
	@Override
	public PageUtil loadOrgList(PageParam pageParam) {
		return this.authDao.loadOrgList(pageParam);
	}
	@Override
	public PageUtil loadRoleList(PageParam pageParam) {
		return this.authDao.loadRoleList(pageParam);
	}
	@Override
	public PageUtil loadPowerList(PageParam pageParam) {
		return this.authDao.loadPowerList(pageParam);
	}
	public Pagination loadUserList(PageParams pageParams,String loginId,String userName){
		Finder finder = Finder.create("select a ");
		finder.append(" from CmsUser a where a.id!=1 and a.status=1"); 
		if(StringUtils.isNotBlank(userName)){
			finder.append(" and a.username like:userName"); 
			finder.setParam("userName", "%" + userName + "%");
		}  
		if(StringUtils.isNotBlank(loginId)){
			finder.append(" and a.loginid like:loginId");
			finder.setParam("loginId", "%" + loginId + "%");
		}   
		Pagination page = this.authDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}
	@Override
	public Pagination loadRoleList(PageParams pageParams, String roleName) {
		Finder finder = Finder.create("select a ");
		finder.append(" from CmsRole a where 1=1"); 
		if(StringUtils.isNotBlank(roleName)){
			finder.append(" and a.roleName like:roleName"); 
			finder.setParam("roleName", "%" + roleName + "%");
		}   
		Pagination page = this.authDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}
	@Override
	public Pagination loadOrgList(PageParams pageParams, String orgName) {
		Finder finder = Finder.create("select a ");
		finder.append(" from CmsOrg a where 1=1"); 
		if(StringUtils.isNotBlank(orgName)){
			finder.append(" and a.orgName like:orgName"); 
			finder.setParam("orgName", "%" + orgName + "%");
		}   
		Pagination page = this.authDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}
	@Override
	public Pagination loadPowerList(PageParams pageParams, String powerName) {
		Finder finder = Finder.create("select a ");
		finder.append(" from CmsPower a where 1=1"); 
		if(StringUtils.isNotBlank(powerName)){
			finder.append(" and a.powerName like:powerName"); 
			finder.setParam("powerName", "%" + powerName + "%");
		}   
		Pagination page = this.authDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}

	 
}